//
//  NEWfeatureController.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/23.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import UIKit
import SnapKit
class NEWfeatureController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       
    }

}
extension NEWfeatureController:UICollectionViewDataSource{
    //有多少组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
        return 1
    }
    //有多少行
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 4
    }
    //每一行显示的cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        //设置每一行的cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NEWfeatureControllercell, forIndexPath: indexPath) as! NEWfeatureControllerCell
        
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.redColor() : UIColor.purpleColor()
        //设置数据
        cell.index = indexPath.item
        return cell
        
    }
}

extension NEWfeatureController:UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //手动获取当前显示的cell对应的index
        let index = collectionView.indexPathsForVisibleItems().last!
        JWLog(index.item)
        
        let currentCell = collectionView.cellForItemAtIndexPath(index) as! NEWfeatureControllerCell
        
        if index.item == 3 {
            currentCell.starAnimation()
        }
    }
    
}


//MARK:自定义cell
//注意需要在storyboard里面的cell的class设置为:NEWfeatureControllerCell
class NEWfeatureControllerCell: UICollectionViewCell {
    var index : Int = 0
        {
        didSet{
            let name = "new_feature_\(index + 1)"
            iconView.image = UIImage(named: name)
            starButton.hidden = true
            
        }
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //初始化UI,设置图片,按钮
    func setupUI()  {
        //添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(starButton)
        //布局
        iconView.snp_makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        starButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView).offset(-150)
        }
    }
    
    //执行动画,按钮弹出效果,在UICollectionViewDelegate方法实现
    
    func starAnimation()  {
        starButton.hidden = false
        starButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        starButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.starButton.transform = CGAffineTransformIdentity
            }) { (true) in
                self.starButton.userInteractionEnabled = true
        }
        
    }
    //MARK:懒加载方法
    
    lazy var iconView = UIImageView()
    lazy var starButton:UIButton = {
        let btn = UIButton(imageName: nil, backgroundImageName: "new_feature_button")
        btn.addTarget(self, action: #selector(NEWfeatureControllerCell.startBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    func startBtnClick()  {
        NSNotificationCenter.defaultCenter().postNotificationName(SwithrootViewController, object: "main")
    }
}

//MARK:自动义布局
//在storyboard中设置collectionview布局属性为"custom",并且设置class为"NEWfeatureLayout"
class NEWfeatureLayout: UICollectionViewFlowLayout
{
    // 准备布局
    override func prepareLayout() {
        //设置尺寸
        itemSize = UIScreen.mainScreen().bounds.size
        //去掉间隙
        minimumLineSpacing = 0
//        minimumInteritemSpacing = 0
        //横向
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
//        sectionInset = UIEdgeInsets(top: 50, left: 100, bottom: 200, right: 400)
        //禁止回弹
        collectionView?.bounces = false
        //分页
        collectionView?.pagingEnabled = true
        //取消进度条
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
}

